#include "AudioQueue.h"

AudioQueue::AudioQueue(int maxCapacity, int sampleRate, int channels)
    : FrameQueue(), maxCapacity(maxCapacity), sampleRate(sampleRate), channels(channels), currentSamples(0) {}

int AudioQueue::calculateFrameSamples(AVFrame *frame) 
{
    if (!frame)
        return 0;

    return frame->nb_samples * channels;
}

void AudioQueue::onFramePushed(AVFrame *frame)
{
    std::unique_lock<std::mutex> lock(m_m);
    currentSamples += calculateFrameSamples(frame);
}

void AudioQueue::onFramePoped(AVFrame *frame)
{
    std::unique_lock<std::mutex> lock(m_m);
    currentSamples -= calculateFrameSamples(frame);
}

bool AudioQueue::isFull() 
{
    std::lock_guard<std::mutex> lock(m_m);
    return currentSamples >= maxCapacity;
}
